/*!
 * jQuery Form Plugin
 * version: 0.2 (17-06-2011)
 * @requires jQuery v1.3.2 or later
 *
 * @author: Mauro Gadaleta [mggadaleta@gmail.com]
 */
;(function($){

    $.fn.zoomEffect = function( userOptions ){

        var settings = ({
            timeAnimationZoomIn: 150,
            timeAnimationZoomOut: 150,
            scalePercentage: 10,
            zoomAnchor: $.fn.zoomEffect.ANCHORCENTER,
            externalControls: {
                enabled: false,
                elements: undefined
            }
        });
        
        var options = $.extend({}, settings, userOptions);

        //prepare list
        $(this).each(function(){
            $(this).css('background-size','cover')
                    .css('-webkit-background-size','cover')
                    .css('-moz-background-size','cover')
                    .css('-o-background-size','cover');
        });

        $(this).hover(function(){
            zoomIn($(this));
        },function(){
            zoomOut($(this));
        });

        var ext = options.externalControls;

        if(ext.enabled)
        {
            ext.elements.hover(function(){
                var idElement = $(this).attr('rel');
                zoomIn($('#'+idElement));
            },function(){
                var idElement = $(this).attr('rel');
                zoomOut($('#'+idElement));
            });
        }

        function zoomIn ( el ){
            if(el.data('ready') || el.data('ready') == undefined)
            {
                el.data('ready',false);
                var oldWidth = el.width();
                var oldHeight = el.height();
                el.data('oldWidth',oldWidth);
                el.data('oldHeight',oldHeight);

                el.data('oldZIndex',el.css('z-index'));
                el.css('z-index',1);

                var valPer = (1+(0.01*options.scalePercentage))

                var newWidth = oldWidth*valPer;
                var newHeight = oldHeight*valPer;

                var sizes = options.zoomAnchor( newWidth , oldWidth , newHeight , oldHeight );

                el.stop().animate({
                    width: newWidth,
                    height: newHeight,
                    marginLeft: sizes.marginLeft,
                    marginTop: sizes.marginTop
                },options.timeAnimationZoomIn);
            }
        }

        function zoomOut ( el ) {
            el.animate({
                width: el.data('oldWidth'),
                height: el.data('oldHeight'),
                marginLeft: 0,
                marginTop: 0
            },options.timeAnimationZoomOut,function(){
                el.css('z-index',el.data('oldZIndex'));
                el.data('ready',true);
            });
        }
    };

    $.fn.zoomEffect.ANCHORCENTER = function( newWidth , oldWidth , newHeight , oldHeight ){
        var marginLeft = -((newWidth-oldWidth)/2);
        var marginTop = -((newHeight-oldHeight)/2);
        return {marginLeft:marginLeft,marginTop:marginTop};
    };
    $.fn.zoomEffect.ANCHORTOPLEFT = function( newWidth , oldWidth , newHeight , oldHeight ){
        var marginLeft = 0;
        var marginTop = 0;
        return {marginLeft:marginLeft,marginTop:marginTop};
    };
    $.fn.zoomEffect.ANCHORTOPRIGHT = function( newWidth , oldWidth , newHeight , oldHeight ){
        var marginLeft = -(newWidth-oldWidth);
        var marginTop = 0;
        return {marginLeft:marginLeft,marginTop:marginTop};
    };
    $.fn.zoomEffect.ANCHORBOTTOMLEFT = function( newWidth , oldWidth , newHeight , oldHeight ){
        var marginLeft = 0;
        var marginTop = -(newHeight-oldHeight);
        return {marginLeft:marginLeft,marginTop:marginTop};
    };
    $.fn.zoomEffect.ANCHORBOTTOMRIGHT = function( newWidth , oldWidth , newHeight , oldHeight ){
        var marginLeft = -(newWidth-oldWidth);
        var marginTop = -(newHeight-oldHeight);
        return {marginLeft:marginLeft,marginTop:marginTop};
    };
    
})(jQuery);